/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package giuliani_mapelli_marocco.business;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;


public class TimeTools {
    
     /**
     * This method will provide a List with all the dates between the specified parameters
     * @param start  is the starting date of the interval
     * @param end the final date of the interval
     * @return The list of dates between the specified ones, including the start and the ending date 
     */
    public List<Date> getDateList(Date start, Date end) {
        
        List<Date> date = new ArrayList();
        Date toadd = start;
        while(toadd.before(end)) {
            date.add(toadd);
            toadd = addDays(toadd, 1);  
        }
        date.add(end);//this is not included in the execution of while
        for(Date d : date) {
            System.out.print(d.toString());
        }
        return date;
    }
    
    /**
     * Increment the date by adding specified days to the beginning one
     * @param date the starting date
     * @param days the number of days to add (or to subtract) to the current date+
     * @return a new date, incremented (or decremented) by the specified number of days.
     */
    public static Date addDays(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days); //minus number would decrement the days
        return cal.getTime();
    }
    
     /**
     * This method will give the date of today.
     * @return current date 
     */
    public Date today() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), 0, 0, 0);
 
        return calendar.getTime();
    }
    
     /**
     * This method is used to change format to the calendar
     * @param d the date to change
     * @return the date with a different format
     */
    public Date changeDateFormat(Date d) {
        int year = d.getYear();
        int month = d.getMonth();
        int day = d.getDate();
        return new Date(year, month, day);
    } 
    
    /**
     *merges a date yyyy-MM-dd and an hour hh-mm in a single date yyyy-MM-dd-hh-mm
     * @param date date yyyy-MM-dd to be merged
     * @param hour hour hh-mm to be merged
     * @return the merged date in format yyyy-MM-dd-hh-mm
     */
    public Date mergeDateAndHour(Date date, Date hour) {
       int year = (int) date.getYear();
        int day = (int) date.getDate();
        int month = (int) date.getMonth();
        int min = (int) hour.getMinutes(); 
        int hrs = (int) hour.getHours();
        return new Date(year, month, day, hrs, min);
   } 
    
     /**
     * This method will return the number of days from which the event is formed
     * @param start the starting date of the event 
     * @param end the ending date of the event
     * @return the number of days od the event
     */
    public long daysDiff(Date start, Date end) {
        
        long diff = end.getTime() - start.getTime();
        
        long diffDays = diff / (24 * 60 * 60 * 1000);
        
        return (diffDays+1);
    }
    
    /**
     * This method will return a Date in String in a different format   
     * @param date The date to parse
     * @return String of the parsed date
     */
    public static String convertDateToString(Date date) {
        
        SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy", Locale.UK);
        String newDate = formatter.format(date);
        return newDate;
    }
    
}
    

